package com.qk.management.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public PageResult<Course> list(Course course) {
        PageHelper.startPage(course.getPage(), course.getPageSize());
        List<Course> result = courseMapper.selectByPage(course);
        Page<Course> coursePage = (Page<Course>) result;
        return PageResult.<Course>builder()
                .total((int) coursePage.getTotal())
                .rows(coursePage.getResult())
                .build();
    }

    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }

    @Override
    public void add(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        course.setCreateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }

    @Override
    public Course selectById(Integer id) {
        return courseMapper.selectById(id);
    }

    @Override
    public void update(Course course) {

        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    @Override
    public List<Course> listAll() {
        return courseMapper.listAll();
    }

    @Override
    public List<Course> selectBySubject(Integer subject) {
        return courseMapper.selectBySubject(subject);
    }

}
